$(function(){
	
	$(".gotoedit").on("click",function(){
		if(!$(this).hasClass("disablebtn")){
		$(".previewwork").hide();	
		document.title="编辑作品";
		$(".originalwork").show();
		}
		
	})
	
	$('#previewmain').on('click',".previewitem",function(){
		//改变大的canvas 的布局及图片	
		var index=$(this).index();
		$(".previewmask").hide();
		$(".previewitem").removeClass("editoring")
		
		$(this).addClass("editoring");	
		$(this).find(".previewmask").show();
		var olditem=UtilProperty[work_id].worker[index];
		var item_boxw=olditem.width;
		var item_boxh=olditem.height;
		var w=$(window).width()-24;
		var h=$(window).height()-215;
		var scale;		
			if(item_boxw / item_boxh > w / h) {
				scale=w/item_boxw;
			} else {
				scale=h/item_boxh;
			}
		var imgsty=item_boxw>item_boxh?"width:100%;":"height:100%;";
		//var bg=olditem.background_id!=0?'background-image:url('+olditem.background.image_url+item_boxw*scale+'x'+item_boxh*scale+')':'';
		if($(this).attr("title")=="封面"||$(this).attr("title")=="封底"||$(this).attr("title")=="扉页"){
			$(".editor_current_pre").html($(this).attr("title"));
		}else{	
			$(".editor_current_pre").html('第'+$(this).attr("page")+'页');
		}
		$(this).addClass("editoring").siblings().removeClass("editoring");	
		$("#previewPic").find("img").attr('src',UtilProperty[work_id].base64image[index].imgurl).attr("style",imgsty);
		
 			
	})
})


//预览已经提交的作品
function getpreviewPic(){
	$("#previewPic,#previewmain").empty();
	var item_boxw=UtilProperty[work_id].worker[0].width;
	var item_boxh=UtilProperty[work_id].worker[0].height;	
	var w=$(window).width()-24;
	var h=$(window).height()-215;
	var scale;		
		if(item_boxw / item_boxh > w / h) {
			scale=w/item_boxw;
		} else {
			scale=h/item_boxh;
		}
	if(opentype==1){
		work_tit=$(".onactive").find(".placeholder").attr('title');
	}	

	for(var i=0; i<UtilProperty[work_id].base64image.length;i++){
		var item=UtilProperty[work_id].base64image[i];		
		var olditem=UtilProperty[work_id].worker[i];
		var inner_scale=$("#previewmain").height()/olditem.height;
		var item_h=parseInt(olditem.height*inner_scale);
		var item_w=parseInt(olditem.width*inner_scale);
		/*var divhtm='<div class="previewitem itemflexthree '+(i==0?'editoring':'')+'" id="'+work_id+'_'+olditem.id+'" title="'+item.title+'" page="'+olditem.page+'" style=" height:'+item_h+'px;  width:'+item_w+'px;">'+
						'<div class="preview_item">'+
						//'<div class="page_item '+(i==0?'editoring':'')+'">'+
							'<img src="'+item.imgurl+'" alt="" style="'+(olditem.background_id!=0?'background-image:url('+olditem.background.image_url+item_w+'x'+item_h+')':'')+'">'+
							'<div class="previewmask _absolute" style="display:'+(i==0?'':'none')+'">'+item.title+'</div>'+
						'</div>'+					
					'</div>';*/
		var divhtm='<div class="previewitem itemflexthree '+(i==0?"editoring":"")+'" id="'+work_id+'_'+olditem.id+'" title="'+item.title+'" page="'+olditem.page+'" style=" height:'+item_h+'px;  width:'+item_w+'px;"><img src="'+item.imgurl+'"></div>';
					
		$("#previewmain").append(divhtm);
		
		if(i==0){	
		var imgsty=item_boxw>item_boxh?"width:100%;":"height:100%;";
		
		$("#previewPic").attr("style","width:"+item_boxw*scale+"px;height:"+item_boxh*scale+"px;").html('<img src="'+item.imgurl+'" style="'+imgsty+(olditem.background_id!=0?'background-image:url('+olditem.background.image_url+item_boxw*scale+'x'+item_boxh*scale+')':'')+'" >');
	
		var bgw=Math.floor(item_boxw*scale/16)*16;
		$(".previewwork .preview_top_bg").attr("style","width:"+bgw+"px;");
		
		
		$("#previewPic").attr("page",olditem.page);
		$(".editor_current_pre").html(olditem.title);
			
		}
	}
	if(UtilProperty[work_id].workerInfo.status==0){
			$(".gotoedit").removeClass("disablebtn");
	}else{
		$(".gotoedit").addClass("disablebtn");
	}
	
	$(".previewwork").show();
	document.title="预览作品";
	
}
